package com.oig.sys.base.tax.service.impl;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.oig.common.util.JsonUtil;
import com.oig.sys.base.tax.client.GovGoodsTariffClient;
import com.oig.sys.base.tax.dto.AgreementCountry;
import com.oig.sys.base.tax.dto.GoodsTariffRate;
import com.oig.sys.base.tax.dto.GoodsVatRate;
import com.oig.sys.base.tax.dto.TariffAgreement;
import com.oig.sys.base.tax.service.IGovGoodsTaxService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class GovGoodsTaxServiceImpl implements IGovGoodsTaxService {

    private final GovGoodsTariffClient govGoodsTariffClient;

    @Override
    public GoodsTariffRate getGoodsTariffRate(String hsCode) {
        JSONObject reqObj = new JSONObject();
        reqObj.put("gName","");
        reqObj.put("codeTs",hsCode);
        reqObj.put("sysDate", DateUtil.formatDate(new Date())); //日期格式2024-05-20
        reqObj.put("nextPage","1");
        reqObj.put("pageSize","10");
        String data = reqObj.toJSONString();
        log.debug("req-data:{}", data);
        String respData = govGoodsTariffClient.queryGoodsTariffRate(data);
        log.debug("resp:{}", respData);
        JSONObject jsonObject = JSONObject.parseObject(respData);
        Integer status = jsonObject.getInteger("statue");
        Integer count = jsonObject.getInteger("totalCount");
        if (status == 1 && count == 1){
            List<GoodsTariffRate> rateList = JsonUtil.toList(jsonObject.getString("data"), GoodsTariffRate.class);
            return rateList.get(0);
        }
        return null;
    }

    @Override
    public GoodsVatRate getGoodsVatRate(String hsCode) {
        JSONObject reqObj = new JSONObject();
        reqObj.put("codeTs",hsCode);
        reqObj.put("sysDate", DateUtil.formatDate(new Date())); //日期格式2024-05-20
        String data = reqObj.toJSONString();//URLEncode
        log.debug("req-data:{}", data);
        String respData = govGoodsTariffClient.queryGoodsVatRate(data);
        log.debug("resp:{}", respData);
        JSONObject jsonObject = JSONObject.parseObject(respData);
        Integer status = jsonObject.getInteger("statue");
        Integer count = jsonObject.getInteger("totalCount");
        if (status == 1 && count==1){
            List<GoodsVatRate> rateList = JsonUtil.toList(jsonObject.getString("data"), GoodsVatRate.class);
            return rateList.get(0);
        }
        return null;
    }

    @Override
    public List<TariffAgreement> getTariffAgreementList(String hsCode) {
        JSONObject reqObj = new JSONObject();
        reqObj.put("codeTs",hsCode);
        reqObj.put("sysDate", DateUtil.formatDate(new Date())); //日期格式2024-05-20
        String data = reqObj.toJSONString();//URLEncode
        log.debug("req-data:{}", data);
        String respData = govGoodsTariffClient.queryTariffAgreement(data);
        log.debug("resp:{}", respData);
        JSONObject jsonObject = JSONObject.parseObject(respData);
        Integer status = jsonObject.getInteger("statue");
        Integer count = jsonObject.getInteger("totalCount");
        if (status == 1 && count>0){
            return JsonUtil.toList(jsonObject.getString("data"), TariffAgreement.class);
        }
        return null;
    }

    @Override
    public List<AgreementCountry> getAgreementCountryList(String hsCode, String agreementCode) {
        JSONObject reqObj = new JSONObject();
        reqObj.put("codeTs",hsCode);
        reqObj.put("sysDate", DateUtil.formatDate(new Date())); //日期格式2024-05-20
        reqObj.put("agreementCode",agreementCode);
        String data = reqObj.toJSONString();
        log.debug("req-data:{}", data);
        String respData =  govGoodsTariffClient.queryAgreementCountry(data);
        log.debug("resp:{}", respData);
        JSONObject jsonObject = JSONObject.parseObject(respData);
        Integer status = jsonObject.getInteger("statue");
        Integer count = jsonObject.getInteger("totalCount");
        if (status == 1 && count>0){
            return JsonUtil.toList(jsonObject.getString("data"), AgreementCountry.class);
        }
        return null;
    }


}
